<?php
global $_GPC, $_W;
$weid = $this->_weid;
$action = 'reservation';
$title = $this->actions_titles[$action];

$storeid = intval($_GPC['storeid']);
//检查门店
$this->checkStore($storeid);
$title = $this->actions_titles[$action];
$returnid = $this->checkPermission($storeid);
//当前门店
$cur_store = $this->getStoreById($storeid);
//设置菜单
$GLOBALS['frames'] = $this->getNaveMenu($storeid,$action);

$tablezones = pdo_fetchall("SELECT * FROM " . tablename($this->table_tablezones) . " WHERE weid = :weid AND storeid=:storeid ORDER BY displayorder DESC", array(':weid' => $weid, ':storeid' => $storeid));
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';

if ($operation == 'post') {
    $id = intval($_GPC['id']);
    $tablezonesid = intval($_GPC['tablezonesid']);

    if (!empty($id)) {
        $item = pdo_fetch("SELECT * FROM " . tablename($this->table_reservation) . " WHERE id = :id", array(':id' => $id));
        if (empty($item)) {
            message('抱歉，数据不存在或是已经删除！', '', 'error');
        }
    }

    if (checksubmit('submit')) {
        $data = array(
            'weid' => intval($weid),
            'storeid' => $storeid,
            'tablezonesid' => intval($_GPC['tablezonesid']),
            'time' => trim($_GPC['time']),
            'dateline' => TIMESTAMP,
        );

        if (empty($id)) {
            pdo_insert($this->table_reservation, $data);
        } else {
            unset($data['dateline']);
            pdo_update($this->table_reservation, $data, array('id' => $id, 'weid' => $weid));
        }
        message('操作成功！', $this->createWebUrl('reservation', array('op' => 'display', 'storeid' => $storeid)), 'success');
    }
} else if ($operation == 'batch') {
    $tablezonesid = intval($_GPC['tablezonesid']);
    if (checksubmit('submit')) {
        $timepoint = intval($_GPC['time_point']);
        $timecount = intval($_GPC['time_count']);
        $time = trim($_GPC['time']);

        if (empty($time)) {
            message('请输入起始时间点！', '', 'error');
        }
        if ($timecount <= 0) {
            message('创建数量不能小于0！', '', 'error');
        } else if ($timecount > 15) {
            message('创建数量不能大于15！', '', 'error');
        }

        $t = strtotime($time);
        for ($i = 0; $i < $timecount; $i++) {
            $time = date('H:i', $t);

            $ishave = pdo_fetch("SELECT * FROM " . tablename($this->table_reservation) . " WHERE weid = :weid AND storeid = :storeid AND tablezonesid = :tablezonesid AND time=:time", array(':weid' => $weid, ':storeid' => $storeid, ':tablezonesid' => $tablezonesid, ':time' => $time));

            $data = array(
                'weid' => $weid,
                'storeid' => $storeid,
                'tablezonesid' => $tablezonesid,
                'time' => $time,
                'dateline' => TIMESTAMP,
            );
            if (empty($ishave)) {
                pdo_insert($this->table_reservation, $data);
            }
            $t = strtotime($time) + $timepoint * 60;
        }
        message('操作成功！', $this->createWebUrl('reservation', array('op' => 'display', 'storeid' => $storeid)), 'success');
    }
} else if ($operation == 'display') {
    if (!empty($_GPC['displayorder'])) {
        foreach ($_GPC['displayorder'] as $id => $displayorder) {
            pdo_update($this->table_reservation, array('displayorder' => $displayorder), array('id' => $id));
        }
        message('排序更新成功！', $this->createWebUrl('reservation', array('op' => 'display', 'storeid' => $storeid)), 'success');
    }

    $tablezones = pdo_fetchall("SELECT * FROM " . tablename($this->table_tablezones) . " WHERE weid = :weid AND storeid=:storeid ORDER BY displayorder DESC", array(':weid' => $weid, ':storeid' => $storeid), 'id');
    $pindex = max(1, intval($_GPC['page']));
    $psize = 15;
    $list = pdo_fetchall("SELECT * FROM " . tablename($this->table_reservation) . " WHERE weid = :weid AND storeid =:storeid ORDER BY
displayorder DESC,id ASC LIMIT
" . ($pindex - 1) * $psize . ',' . $psize, array(':weid' => $this->_weid, ':storeid' => $storeid));
    $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename($this->table_reservation) . " WHERE weid = :weid AND storeid =:storeid ", array(':weid' => $this->_weid, ':storeid' => $storeid));
    $pager = pagination($total, $pindex, $psize);
} else if ($operation == 'delete') {
    $id = intval($_GPC['id']);
    $row = pdo_fetch("SELECT id FROM " . tablename($this->table_reservation) . " WHERE id = :id", array(':id' => $id));
    if (empty($row)) {
        message('抱歉，数据不存在或是已经被删除！');
    }

    pdo_delete($this->table_reservation, array('id' => $id, 'weid' => $weid));
    message('操作成功！', $this->createWebUrl('reservation', array('op' => 'display', 'storeid' => $storeid)), 'success');
}

include $this->template('web/reservation');